import sys
import os
import cropimage

def cropall(src_dir):
    flist = os.listdir(src_dir)
    for f in flist:
        path = os.path.join(src_dir, f)
        file_ext = f.rsplit('.',maxsplit=1)
        if len(file_ext)!=2:
            continue
        if file_ext[1]!='jpg' and file_ext[1]!='jpeg' and file_ext[1]!='png' and file_ext[1]!='JPG' and file_ext[1]!='PNG':
            continue
        cropimage.crop(path, True)

def main():
    if len(sys.argv)!=2:
        print("参数错误，请输入文件夹名称\n")
        exit()
    ori_dir = sys.argv[1]
    if not os.path.isdir(ori_dir):
        print("目标参数不是文件夹\n")
        exit()

    flist = os.listdir(ori_dir)

    for f in flist:
        subpath = os.path.join(ori_dir, f)
        if os.path.isdir(subpath):
            subflist = os.listdir(subpath)
            for subf in subflist:
                cf = os.path.basename(subpath)
                trdpath = os.path.join(subpath, subf)
                if os.path.isdir(trdpath) and subf==cf:
                    print(trdpath)
                    cropall(trdpath)


if __name__ == '__main__':
    main()